#include "PathManager.h"

PathManager::PathManager(Map* map, GraphManager* graphManager, string idEdgeFrom, int addFrom, string idEdgeTo, int addTo, List* objects, int option)
{
	this->graphManager = graphManager;
	this->map = map;
	this->idEdgeFrom = idEdgeFrom;
	this->addFrom = addFrom;
	this->idEdgeTo = idEdgeTo;
	this->addTo = addTo;
	this->objects = objects;
	this->option = option;
}

PathManager::~PathManager(void)
{
}

List* PathManager::getPathToDraw(string idEdgeFrom, int numberFrom) {
	this->idEdgeFrom = idEdgeFrom;
	this->addFrom = numberFrom;
	List* path = NULL;
	// if you're looking for a street
	if (objects == NULL) {
		
		if(this->idEdgeFrom.compare(this->idEdgeTo) == 0){
			Edge *edge = map->getGraph()->getEdge(this->idEdgeTo);
			if(!(edge->getLastAddress() > edge->getInitAddress() && (this->addTo - this->addFrom) > 0)
				&& !(edge->getLastAddress() > edge->getInitAddress() && (this->addTo - this->addFrom) > 0)){
				if (option == 2) {
					path = graphManager->getFastestPathFromEdges(this->idEdgeFrom, this->idEdgeTo);
				}
				if (option == 3) {
					path = graphManager->getShortestPathFromEdges(this->idEdgeFrom, this->idEdgeTo);
				}
			}
		}else{
			if (option == 2) {
				path = graphManager->getFastestPathFromEdges(this->idEdgeFrom, this->idEdgeTo);
			}
			if (option == 3) {
				std::cout << "Calc path === From: " << idEdgeFrom << " To: " << this->idEdgeTo << std::endl;
				path = graphManager->getShortestPathFromEdges(this->idEdgeFrom, this->idEdgeTo);
			}
		}
	} else {

		MapObject* near = new MapObject();
		
		if (option == 1) {
			path = graphManager->getFastestPathFromEdgeToBuilding(this->idEdgeFrom, this->addFrom, objects, *near);
		}
		if (option == 2) {
			path = graphManager->getClosestPathFromEdgeToBuilding(this->idEdgeFrom, this->addFrom, objects, *near);
		}

		this->idEdgeTo = near->getIdEdge();
		this->addTo = near->getNumber();
	}

	return path;
}

string PathManager::getIdEdgeTo() {
	return this->idEdgeTo;
}

string PathManager::getIdEdgeFrom() {
	return this->idEdgeFrom;
}

int PathManager::getAddTo() {
	return this->addTo;
}
